package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class SetmealServiceImpl implements SetmealService {


    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;


    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.save(setmeal);
        Long id = setmeal.getId();

        List<SetmealDish> setmealDishes =setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
        setmealDishMapper.saveSetmealDishes(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
      //起售中的套餐不能删除
        for (Long id : ids) {
          Setmeal setmeal= setmealMapper.getById(id);
          if (setmeal.getStatus()== StatusConstant.ENABLE){
              throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
          }

        }
       setmealDishMapper.deleteBatch(ids);
        setmealMapper.deleteBatch(ids);
    }

    /*8
    根据id查询套餐
     */
    @Override
    public SetmealVO getByIdWithDishes(Long id) {
        Setmeal setmeal= setmealMapper.getByID(id);
        SetmealVO setmealVO= new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> list=setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        ArrayList<Long> ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        setmealDishMapper.deleteBatch(ids);
        List<SetmealDish> setmealDishes =setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.saveSetmealDishes(setmealDishes);
    }

    /**
     * 起售和停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        if (status==StatusConstant.ENABLE){
            Category category= setmealMapper.getCategoryById(id);
            if (category.getStatus()==StatusConstant.DISABLE){
                throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED_CATEGORY_DISABLE);
            }
            List<DishVO> list =dishMapper.getBySetmealId(id);
            for (DishVO dishVO : list) {
                if (dishVO.getStatus()==StatusConstant.DISABLE){
                    throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal=Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }


    /**
     * 根据条件查询套餐
     */
    public List<Setmeal> list(Setmeal setmeal){
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;

    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        //查询套餐内部的图喝描述

        return setmealMapper.getDishItemBySetmealId(id);
    }

}
